﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class ChupHinhBO : Csla.BusinessBase<ChupHinhBO>
    {
        #region Business Properties
        //CHUPHINHID
        private int? _ChupHinhID = null;
        public int? ChupHinhID
        {
            get { return this._ChupHinhID; }
            set { this._ChupHinhID = value; PropertyHasChanged("ChupHinhID"); }
        }

        //MA
        private string _Ma = null;
        public string Ma
        {
            get { return this._Ma; }
            set { this._Ma = value; PropertyHasChanged("Ma"); }
        }

        //TEN
        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; PropertyHasChanged("Ten"); }
        }

        //GIATIEN
        private double? _GiaTien = 0;
        public double? GiaTien
        {
            get { return this._GiaTien; }
            set { this._GiaTien = value; PropertyHasChanged("GiaTien"); }
        }

        //CONSUDUNG
        private bool? _ConSuDung = true;
        public bool? ConSuDung
        {
            get { return this._ConSuDung; }
            set { this._ConSuDung = value; PropertyHasChanged("ConSuDung"); }
        }

        //LANSUDUNG
        private int? _LanSuDung = 0;
        public int? LanSuDung
        {
            get { return this._LanSuDung; }
            set { this._LanSuDung = value; PropertyHasChanged("LanSuDung"); }
        }

        //DELE
        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }


        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        protected override object GetIdValue()
        {
            return _ChupHinhID.Value;
        }

        private static int newChupHinhID = -1;
        private ChupHinhBO()
        {
            _ChupHinhID = newChupHinhID--;
        }

        public ChupHinhBO(FbDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public ChupHinhBO(FbDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static ChupHinhBO NewRootChupHinhBO()
        {
            return DataPortal.Create<ChupHinhBO>(new RootCriteria());
        }

        internal static ChupHinhBO NewChildChupHinhBO()
        {
            return DataPortal.Create<ChupHinhBO>(new ChildCriteria());
        }

        public static ChupHinhBO GetRootChupHinhBO(int _ChupHinhID)
        {
            return DataPortal.Fetch<ChupHinhBO>(new RootCriteria(_ChupHinhID));
        }

        public static ChupHinhBO GetRootChupHinhBOByWhere(string where)
        {
            return DataPortal.Fetch<ChupHinhBO>(where);
        }

        internal static ChupHinhBO GetChildChupHinhBO(FbDataReader dataReader)
        {
            return new ChupHinhBO(dataReader);
        }

        public static void DeleteChupHinhBO(int _ChupHinhID)
        {
            DataPortal.Delete(new RootCriteria(_ChupHinhID));
        }

        public static void DeleteChupHinhBO(int _ChupHinhID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_ChupHinhID, directDelete));
        }

        public static string GetIDChupHinhBO()
        {
            return "ChupHinhID";
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int ChupHinhID;
            public bool directDelete = false;
            public RootCriteria(int _ChupHinhID)
            {
                this.ChupHinhID = _ChupHinhID;
                this.directDelete = false;
            }

            public RootCriteria(int _ChupHinhID, bool directDelete)
            {
                this.ChupHinhID = _ChupHinhID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int ChupHinhID;
            public bool directDelete = false;
            public ChildCriteria(int _ChupHinhID)
            {
                this.ChupHinhID = _ChupHinhID;
                this.directDelete = false;
            }

            public ChildCriteria(int _ChupHinhID, bool directDelete)
            {
                this.ChupHinhID = _ChupHinhID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "ChupHinhBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = "CHUPHINHID = " + criteria.ChupHinhID;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void This_Fetch(string where)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "ChupHinhBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = where;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void Fetch(FbDataReader dataReader)
        {
            if (!(dataReader["CHUPHINHID"] is DBNull))
                this._ChupHinhID = (int)dataReader["CHUPHINHID"];
            if (!(dataReader["MA"] is DBNull))
                this._Ma = ((string)dataReader["MA"]).Trim();
            if (!(dataReader["TEN"] is DBNull))
                this._Ten = ((string)dataReader["TEN"]).Trim();
            if (!(dataReader["GIATIEN"] is DBNull))
                this._GiaTien = Convert.ToDouble(dataReader["GIATIEN"]);
            if (!(dataReader["CONSUDUNG"] is DBNull))
                this._ConSuDung = (((string)dataReader["CONSUDUNG"]).Trim() == "1");
            if (!(dataReader["LANSUDUNG"] is DBNull))
                this._LanSuDung = (int)dataReader["LANSUDUNG"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (((string)dataReader["DELE"]).Trim() == "1");

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "ChupHinhBO_I";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@MA", FbDbType.VarChar).Value = this._Ma;
                    cmd.Parameters.Add("@TEN", FbDbType.VarChar).Value = this._Ten;
                    cmd.Parameters.Add("@GIATIEN", FbDbType.Float).Value = this._GiaTien;
                    cmd.Parameters.Add("@CONSUDUNG", FbDbType.Char).Value = (this._ConSuDung.Value ? "1" : "0");
                    cmd.Parameters.Add("@LANSUDUNG", FbDbType.Integer).Value = this._LanSuDung;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    FbParameter pChupHinhID = new FbParameter("@CHUPHINHID", FbDbType.Integer);
                    pChupHinhID.Direction = ParameterDirection.ReturnValue;
                    cmd.Parameters.Add(pChupHinhID);

                    cmd.ExecuteNonQuery();
                    this._ChupHinhID = Convert.ToInt32(cmd.Parameters["@CHUPHINHID"].Value);
                }
            }
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "ChupHinhBO_U";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CHUPHINHID", FbDbType.Integer).Value = this._ChupHinhID;
                    cmd.Parameters.Add("@MA", FbDbType.VarChar).Value = this._Ma;
                    cmd.Parameters.Add("@TEN", FbDbType.VarChar).Value = this._Ten;
                    cmd.Parameters.Add("@GIATIEN", FbDbType.Float).Value = this._GiaTien;
                    cmd.Parameters.Add("@CONSUDUNG", FbDbType.Char).Value = (this._ConSuDung.Value ? "1" : "0");
                    cmd.Parameters.Add("@LANSUDUNG", FbDbType.Integer).Value = this._LanSuDung;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_ChupHinhID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    if (criteria.directDelete)
                    {
                        cmd.CommandText = "DELETE FROM CHUPHINH WHERE CHUPHINHID = " + criteria.ChupHinhID + ";";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        cmd.CommandText = "UPDATE CHUPHINH SET CHUPHINH.DELE = '1' WHERE CHUPHINHID = " + criteria.ChupHinhID + ";";
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_ChupHinhID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}